package com.soft863.tc.analysis.mapper;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;

import java.io.IOException;

public class AnalysisTextMapper extends TableMapper<Text, Text> {
    private final Text k = new Text();
    private final Text v = new Text();

    @Override
    protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
        String rowKey = Bytes.toString(key.get());
        //5_19602240179_20200311 20:59:36_18101213362_1800_1
        String[] values = rowKey.split("_");
        String calling = values[1];
        String called = values[3];
        String callTime = values[2];
        String duration = values[4];

        String year = callTime.substring(0, 4);
        String month = callTime.substring(0, 6);
        String day = callTime.substring(0, 8);

        //主叫-年
        k.set(calling + "_" + year);
        v.set(duration);
        context.write(k, v);
        //主叫-月
        k.set(calling + "_" + month);
        context.write(k, v);
        //主叫-日
        k.set(calling + "_" + day);
        context.write(k, v);

        //被叫-年
        k.set(called + "_" + year);
        context.write(k, v);
        //被叫-月
        k.set(called + "_" + month);
        context.write(k, v);
        //被叫-日
        k.set(called + "_" + day);
        context.write(k, v);

    }
}
